package org.example.lethear.repository;

import org.example.lethear.entity.Like;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 点赞数据访问接口
 */
@Repository
public interface LikeRepository extends JpaRepository<Like, Long> {
    
    /**
     * 根据用户ID和目标ID及类型查找点赞记录
     */
    Optional<Like> findByUserIdAndTargetIdAndTargetType(Long userId, Long targetId, Integer targetType);
    
    /**
     * 检查用户是否已点赞
     */
    boolean existsByUserIdAndTargetIdAndTargetType(Long userId, Long targetId, Integer targetType);
    
    /**
     * 根据用户ID查找点赞记录
     */
    List<Like> findByUserId(Long userId);
    
    /**
     * 根据用户ID分页查找点赞记录
     */
    Page<Like> findByUserId(Long userId, Pageable pageable);
    
    /**
     * 根据目标ID和类型查找点赞记录
     */
    List<Like> findByTargetIdAndTargetType(Long targetId, Integer targetType);
    
    /**
     * 根据目标类型查找点赞记录
     */
    Page<Like> findByTargetType(Integer targetType, Pageable pageable);
    
    /**
     * 统计某个目标的点赞数
     */
    @Query("SELECT COUNT(l) FROM Like l WHERE l.targetId = :targetId AND l.targetType = :targetType")
    long countByTargetIdAndTargetType(@Param("targetId") Long targetId, @Param("targetType") Integer targetType);
    
    /**
     * 统计用户的点赞数
     */
    @Query("SELECT COUNT(l) FROM Like l WHERE l.userId = :userId")
    long countByUserId(@Param("userId") Long userId);
    
    /**
     * 统计用户在指定时间段内的点赞数
     */
    @Query("SELECT COUNT(l) FROM Like l WHERE l.userId = :userId AND l.createTime BETWEEN :startTime AND :endTime")
    long countByUserIdAndCreateTimeBetween(@Param("userId") Long userId, 
                                          @Param("startTime") LocalDateTime startTime, 
                                          @Param("endTime") LocalDateTime endTime);
    
    /**
     * 查找用户点赞的帖子
     */
    @Query("SELECT l FROM Like l WHERE l.userId = :userId AND l.targetType = 1 ORDER BY l.createTime DESC")
    Page<Like> findUserLikedPosts(@Param("userId") Long userId, Pageable pageable);
    
    /**
     * 查找用户点赞的评论
     */
    @Query("SELECT l FROM Like l WHERE l.userId = :userId AND l.targetType = 2 ORDER BY l.createTime DESC")
    Page<Like> findUserLikedComments(@Param("userId") Long userId, Pageable pageable);
    
    /**
     * 查找最新点赞记录
     */
    @Query("SELECT l FROM Like l ORDER BY l.createTime DESC")
    Page<Like> findRecentLikes(Pageable pageable);
    
    /**
     * 统计今日点赞数
     */
    @Query("SELECT COUNT(l) FROM Like l WHERE l.createTime >= :startOfDay")
    long countTodayLikes(@Param("startOfDay") LocalDateTime startOfDay);
    
    /**
     * 删除用户对某个目标的点赞
     */
    void deleteByUserIdAndTargetIdAndTargetType(Long userId, Long targetId, Integer targetType);
    
    /**
     * 根据目标ID批量删除点赞记录（如删除帖子时）
     */
    void deleteByTargetIdAndTargetType(Long targetId, Integer targetType);
    
    /**
     * 查找热门内容（根据点赞数排序）
     */
    @Query("SELECT l.targetId, l.targetType, COUNT(l) as likeCount FROM Like l GROUP BY l.targetId, l.targetType ORDER BY likeCount DESC")
    Page<Object[]> findHotContent(Pageable pageable);
} 